package TimeSqueeze.logic;
import java.text.ParseException;
import TimeSqueeze.common.*;

// By James Ho

public class Parser {
	TaskInterface inputTask;
	String inputTaskType;
	String userCommand;
	String userDisplayParas;
	String userDeleteParas;
	String userSearchParas;
	boolean userSearchingTime = true;
	String[] userInputParts;
	boolean invalidInput = false;
	
	public Parser(String userInput) {
		userInputParts = userInput.split("-");
		userCommand = userInputParts[0].toLowerCase();
	}
	
	public void parseTaskParameters() {
				// To be used as the format for all date&time entry
				//SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH mm");
				
				switch (userInputParts[1].toLowerCase()) {
				case "timed":
					Time startTime;
					String[] parseStartTime = userInputParts[2].split(" ");
					startTime = new Time(Integer.parseInt(parseStartTime[0]),Integer.parseInt(parseStartTime[1]),Integer.parseInt(parseStartTime[2]),Integer.parseInt(parseStartTime[3]));
					Time endTime;
					String[] parseEndTime = userInputParts[3].split(" ");
					endTime = new Time(Integer.parseInt(parseEndTime[0]),Integer.parseInt(parseEndTime[1]),Integer.parseInt(parseEndTime[2]),Integer.parseInt(parseEndTime[3]));
					TimedTask userInputTimedTask = new TimedTask(startTime,endTime,userInputParts[4],userInputParts[5]);
					inputTask = userInputTimedTask;
					break;
				case "untimed":
					Task userInputTask = new Task(userInputParts[2],userInputParts[3]);
					inputTask = userInputTask; 
					break;
				case "deadline":
					Time endingTime;
					String[] parseEndingTime = userInputParts[2].split(" ");
					endingTime = new Time(Integer.parseInt(parseEndingTime[0]),Integer.parseInt(parseEndingTime[1]),Integer.parseInt(parseEndingTime[2]),Integer.parseInt(parseEndingTime[3]));
					DeadlineTask userInputDeadlineTask = new DeadlineTask(userInputParts[3],endingTime,userInputParts[4]);
					inputTask = userInputDeadlineTask; 
					break;
				default:
					System.out.println("Invalid user input");
					break;
				
				}
	}
	
	public void parseDisplayInput() {
		try {
			userDisplayParas = userInputParts[1];
		}
		catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Please input your display parameter. Eg. display-all");
			invalidInput = true;
		}
	}
	
	public void parseDeleteInput() {
		try {
			userDeleteParas = userInputParts[1];			
		}
		catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Please input your delete parameter. Eg. delete-clear");
			invalidInput = true;
		}
	}
	
	public Time parseSearchInput() {
		try {
			userSearchParas = userInputParts[1];
			
			Time searchTime;
			String[] parseSearchTime = userInputParts[1].split(" ");
			
			 try {
				 searchTime = new Time(Integer.parseInt(parseSearchTime[0]),Integer.parseInt(parseSearchTime[1]),Integer.parseInt(parseSearchTime[2]),Integer.parseInt(parseSearchTime[3]));
				 return searchTime;
			 }
			 catch (NumberFormatException e) {
				 userSearchingTime = false;
				 return null;
			 }
		}
		catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Please input your search parameter. Eg. search-taskdescription");
			invalidInput = true;
			return null;
		}
	}
	
	public void sortInputCommand() throws ParseException {
		switch (userCommand) {
		case "add":
			//inputTaskType = inputTask.getTaskType();
			if (userInputParts[1].toLowerCase().equals("timed")) {
				this.parseTaskParameters();
				if (!invalidInput) {
					AddTimedCommand atc = new AddTimedCommand(inputTask);
					atc.execute();
					System.out.println("added to TimedTask storage");
				}
			}
			if (userInputParts[1].toLowerCase().equals("untimed")) {
				this.parseTaskParameters();
				if (!invalidInput) {
					AddUntimedCommand auc = new AddUntimedCommand(inputTask);
					auc.execute();
					System.out.println("added to To-Do list.");
				}
			}
			if (userInputParts[1].toLowerCase().equals("deadline")) {
				this.parseTaskParameters();
				if (!invalidInput) {
					AddDeadlineCommand adc = new AddDeadlineCommand(inputTask);
					adc.execute();
					System.out.println("added to Deadline storage");
				}
			}
			break;
			
		case "delete":
			this.parseDeleteInput();
			if (!invalidInput) {
				if (userDeleteParas.toLowerCase().equals("clear")) {
					Delete userClearCommand = new Delete(true);
					userClearCommand.execute();
				}
				else {
					// No need to input a full Task to pass in, so redundant.
					// Delete only requires a description/tag content
					/*this.parseTaskParameters();
				if (userInputParts[1].toLowerCase().equals("timed")) {
					Delete userDeleteCommand = new Delete((TimedTask) inputTask);
					userDeleteCommand.execute();
				}
				if (userInputParts[1].toLowerCase().equals("untimed")) {
					Delete userDeleteCommand = new Delete((Task) inputTask);
					userDeleteCommand.execute();
				}
				if (userInputParts[1].toLowerCase().equals("deadline")) {
					Delete userDeleteCommand = new Delete((DeadlineTask) inputTask);
					userDeleteCommand.execute();
				}*/

					Delete userDeleteCommand = new Delete(new Task(userDeleteParas," "));
					userDeleteCommand.execute();
				}
			}
			break;
			
		case "search":
			Time searchTime = this.parseSearchInput();
			if (!invalidInput) {
				if (userSearchingTime) {
					Search userSearchingViaTime = new Search(new DeadlineTask(" ",searchTime," "));
					userSearchingViaTime.execute();
				}
				else {
					Search userSearchingViaDesc = new Search(new Task(userSearchParas," "));
					userSearchingViaDesc.execute();
				}
			}
			break;
			
		case "display":
			this.parseDisplayInput();
			if (!invalidInput) {
				Display displayCommand = new Display(userDisplayParas);
				displayCommand.execute();
			}			
			break;
			
		default:
			System.out.println("Invalid or unimplmented user command");
			break;
		}
	}
	

}
